#include "rtc.h"
#include "ui_rtc.h"
#include "control.h"
#include "QDebug"

#include <QByteArray>
#include <QTextCodec>


rtc::rtc(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::rtc)
{

extern void tim_change();

    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);



}

rtc::~rtc()
{
    delete ui;
}

void rtc::time_play()
{
    extern QByteArray getcmd;
    extern   QByteArray TCP_buff;
    extern QSerialPort *serial;


    extern int tcp_state;
    char * tim_p = NULL;
    static int state =0;



    if(serial->isOpen()==true)
    {
        tim_p = strstr(getcmd,"NOW");
        if(tim_p != NULL)
        {
            state=0;
        }
        else
        {
            state=1;
        }
        if(state%2 == 0)
        {
            tim_p = strstr(getcmd,"NOW");
            if(tim_p != NULL)
            {
                tim_p+=3;
                ui->textEdit->setText(tim_p);
            }
        }
        else
        {
              ui->textEdit->append(getcmd);
        }
    }





    if(tcp_state == 1)
    {
       tim_p = strstr(TCP_buff,"NOW");
       if(tim_p != NULL)
       {
           state=0;
       }
       else
       {
           state=1;
       }
       if(state%2 == 0)
       {
           tim_p = strstr(TCP_buff,"NOW");
           if(tim_p != NULL)
           {
               tim_p+=3;
               ui->textEdit->setText(tim_p);
           }
       }
       else
       {
             ui->textEdit->append(TCP_buff);
       }

    }





}




void rtc::on_pushButton_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;

    extern int tcp_state;

    qDebug() << tcp_state << endl;

     int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }

       }
       if(tcp_state == 1)
       {
           qtcpsocket->write("@close .");

       }

       this->close();
}





void rtc::on_pushButton_2_clicked()
{
    extern QSerialPort *serial;


    if(serial->isOpen()==true)
    {
     serial->write("@");
     serial->write("year");
     serial->write(ui->year->text().toUtf8());
     serial->write("_");

     serial->write("month");
     serial->write(ui->month->text().toUtf8());
     serial->write("_");

     serial->write("day");
     serial->write(ui->day->text().toUtf8());
     serial->write("_");

     serial->write("hour");
     serial->write(ui->hour->text().toUtf8());
     serial->write("_");

     serial->write("minutes");
     serial->write(ui->minutes->text().toUtf8());
     serial->write("_");

     serial->write("seconds");
     serial->write(ui->seconds->text().toUtf8());
     serial->write("_");

     serial->write("weekday");
     serial->write(ui->weekday->text().toUtf8());
     serial->write("_");

     serial->write(" ");
     serial->write(".");
    }

    extern int tcp_state;
    extern QTcpSocket * qtcpsocket;
    if(tcp_state == 1)
    {
     qtcpsocket->write("@");
     qtcpsocket->write("year");
     qtcpsocket->write(ui->year->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("month");
     qtcpsocket->write(ui->month->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("day");
     qtcpsocket->write(ui->day->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("hour");
     qtcpsocket->write(ui->hour->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("minutes");
     qtcpsocket->write(ui->minutes->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("seconds");
     qtcpsocket->write(ui->seconds->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write("weekday");
     qtcpsocket->write(ui->weekday->text().toUtf8());
     qtcpsocket->write("_");

     qtcpsocket->write(" ");
     qtcpsocket->write(".");
    }
}

